package resource

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
)

type Link struct {
	Href    string `json:"href"`
	Content string `json:"content"`
}

type UrlResource struct {
	fileurl string
}

func (u UrlResource) Write(str interface{}) {
	atagjson, err := json.Marshal(str)
	if err != nil {
		fmt.Println("json err: ", err)
	} else {
		ioutil.WriteFile(u.fileurl, atagjson, os.ModeAppend)
	}

}

func (u UrlResource) Read() map[string]map[string]string {
	jsonfile, _ := ioutil.ReadFile(u.fileurl)
	var urlMap map[string]map[string]string
	json.Unmarshal(jsonfile, &urlMap)
	if urlMap == nil {
		return map[string]map[string]string{}
	}
	return urlMap
}

func NewUrlResource() *UrlResource {
	u := &UrlResource{}
	u.fileurl = "data/url.json"
	return u
}
